package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import cn.wolfcode.trip.base.util.SensiWordUtil;
import cn.wolfcode.trip.base.util.UserContext;
import cn.wolfcode.trip.base.vo.CommentTreeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：用户评论控制器
 *
 * @package: cn.wolfcode.trip.base.service.impl
 * @author: wjh
 * @date: 2019/05/30 19:05
 * 总结:
 */
@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private CommentHistoryMapper commentHistoryMapper;
    @Autowired
    private FriendCircleMapper friendCircleMapper;

    @Override
    public void addCommentHistory(CommentHistory commentHistory) {
        if(commentHistory.getPId() == null){
            commentHistory.setPId(0L);
        }
        if(commentHistory.getFormUser() == null){
            commentHistory.setFormUser(UserContext.getCurrentUser());
        }
        commentHistory.setFormUser(UserContext.getCurrentUser());
        //敏感词过滤
        String s = SensiWordUtil.replaceSensitiveWord(commentHistory.getContent(), "*");
        commentHistory.setContent(s);
        commentHistoryMapper.insert(commentHistory);
    }

    @Override
    public void changeStateByUserId(Long qo) {
        commentMapper.changeStateByUserId(qo);
    }

    @Override
    public List<CommentTreeVo> getCommentByUserIdAndType(CommentQueryObject qo) {
        //获取评论的评论
        return listCommentTree(qo);
    }

    @Override
    public List<CommentTreeVo> listCommentTree(CommentQueryObject qo) {
        List<CommentTreeVo> list = null;
        try {
            list = listTree(qo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<CommentTreeVo> listTree(CommentQueryObject qo) {
        CommentTreeVo parent = new CommentTreeVo(0L , "parent");
        addTree(parent, qo);
        List<CommentTreeVo> treeList = new ArrayList<>();
        treeList.add(parent);
        return treeList;
    }

    @Override
    public void addTree(CommentTreeVo parent,CommentQueryObject qo) {
        List<CommentHistory> historyList = commentHistoryMapper.findCommentByParentId(parent.getId(), qo);
        if (historyList != null && historyList.size() > 0) {
            if (parent.getChildren() == null) {
                parent.setChildren(new ArrayList<>());
            }
            for (CommentHistory temp : historyList) {
                CommentTreeVo vo = new CommentTreeVo();
                vo.setId(temp.getId());//id
                vo.setCreateTime(temp.getCreateTime());//评论历史的时间
                vo.setContent(temp.getContent());//评论消息
                User fromUser = temp.getFormUser();
                if (fromUser != null) {
                    vo.setUserId(fromUser.getId());//用户id
                    vo.setFromUserName(fromUser.getNickName());//用户名字
                    vo.setFromUserAvatarUrl(fromUser.getHeadImgUrl());//用户头像
                }
                User toUser = temp.getToUser();
                if (toUser != null) {
                    vo.setToUserId(toUser.getId());//子评论对象id
                    vo.setToUserName(toUser.getNickName());//子评论对象名字
                    vo.setToUserAvatarUrl(toUser.getHeadImgUrl());//头像
                }
                parent.getChildren().add(vo);
                addTree(vo, qo);
            }
        }
    }

    //我的评论
    @Override
    public PageInfo listMyComment(CommentQueryObject qo) {
        //查询朋友圈回复
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize() );
        List<Map> list = commentHistoryMapper.selectFriendCircleMessage(qo);
        //查询攻略评论
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize() );
        List<Map> strategyComments = strategyCommentMapper.selectStrtegyByUserId(qo);
        list.addAll(strategyComments);
        return new PageInfo(list);
    }

    @Override
    public Integer getUnreadComment(QueryObject qo) {
        //评论未读数
        Integer unreadComment = commentHistoryMapper.getUnreadComment(qo);
        //攻略文章未读数
        Integer detailUnread = commentHistoryMapper.getDetailUnread(qo);
        return unreadComment + detailUnread;
    }

    @Override
    public void changeStates(Long userId) {
        //修改攻略文章未读为已读
        commentHistoryMapper.changeDetailStates(userId);
        //查询我的子评论集合
        List<Integral> childrenList =  commentHistoryMapper.selectChilderenComment(userId);
        //修改为读评论为已读
        commentHistoryMapper.changeCommentStates(childrenList);
    }

    @Override
    public PageInfo<Map> listUnreadMessage(CommentQueryObject qo) {
        //查询回复我的攻略消息
        List<Map> strategyMessage = commentHistoryMapper.selectStrategyMessage(qo);
        //查看我的攻略评论的二级评论消息
        List<Map> childrenMessage = commentHistoryMapper.selectChildrenMessage(qo);
        strategyMessage.addAll(childrenMessage);
        return new PageInfo<Map>(strategyMessage);
    }

    @Override
    public void addFriendComment(CommentHistory commentHistory) {
        if(commentHistory.getFormUser() == null){
            commentHistory.setFormUser(UserContext.getCurrentUser());
        }
        commentHistory.setType(2);
        //敏感词过滤
        String s = SensiWordUtil.replaceSensitiveWord(commentHistory.getContent(), "*");
        commentHistory.setContent(s);
        commentHistoryMapper.insert(commentHistory);
    }

}